package com.beehoo.dcportal.admin.service.impl;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.beehoo.dcportal.admin.dao.CommunityDao;
import com.beehoo.dcportal.admin.service.CommunityService;
import com.beehoo.dcportal.common.Constant;
import com.beehoo.dcportal.common.WriteFile;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.common.base.BaseService;
import com.beehoo.dcportal.model.CommunityBean;
import com.beehoo.dcportal.model.FileBean;
import com.beehoo.dcportal.utils.CommonUtils;

public class CommunityServiceImpl extends BaseService implements
		CommunityService {
	
	private CommunityDao communityDao;

	public void setCommunityDao(CommunityDao communityDao) {
		this.communityDao = communityDao;
	}

	@Override
	public void addCommunity(CommunityBean communityBean) throws BaseException {
		validateCommunityBean(communityBean);
		communityBean = disposeUploadFile(communityBean);
		String id = communityDao.addCommunity(communityBean);
		if(CommonUtils.nvlToString(id).equals("")){
			throw new BaseException("custom.messages.error.community.create.error");
		}
	}

	@Override
	public void delCommunity(String communityId) throws BaseException {
		if(CommonUtils.nvlToString(communityId).equals("")){
			throw new BaseException("custom.messages.error.community.id.empty");
		}
		int ret = communityDao.delCommunity(communityId);
		if(ret <= 0){
			throw new BaseException("custom.messages.error.community.del.error");
		}
	}

	@Override
	public List<CommunityBean> queryCommunities(String streetId)
			throws BaseException {
		if(CommonUtils.nvlToString(streetId).equals("")){
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("streetId", streetId);
		return communityDao.queryCommunities(paramMap);
	}

	@Override
	public CommunityBean queryCommunity(String streetId, String communityId)
			throws BaseException {
		if(CommonUtils.nvlToString(streetId).equals("")){
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		if(CommonUtils.nvlToString(communityId).equals("")){
			throw new BaseException("custom.messages.error.community.id.empty");
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("streetId", streetId);
		paramMap.put("communityId", communityId);
		return communityDao.queryCommunity(paramMap);
	}

	@Override
	public void updateCommunity(CommunityBean communityBean)
			throws BaseException {
		validateCommunityBean(communityBean);
		communityBean = disposeUploadFile(communityBean);
		int ret = communityDao.updateCommunity(communityBean);
		if(ret <= 0){
			throw new BaseException("custom.messages.error.community.update.error");
		}
	}

	private void validateCommunityBean(CommunityBean communityBean) throws BaseException{
		if(communityBean == null){
			throw new BaseException("custom.messages.error.community.create.error");
		}
		if(CommonUtils.nvlToString(communityBean.getStreetId()).equals("")){
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		if(CommonUtils.nvlToString(communityBean.getCommunityName()).equals("")){
			throw new BaseException("custom.messages.error.community.name.empty");
		}
	}
	
	private CommunityBean disposeUploadFile(CommunityBean communityBean) throws BaseException{
		FileBean fileBean = communityBean.getFileBean();
		if(fileBean != null){
			int fileSize = fileBean.getFileSize();
			if(fileSize > Constant.UPLOAD_FILE_MAX_SIZE){
				throw new BaseException("struts.messages.error.file.too.large");
			}
			if(!CommonUtils.validateImageFileType(fileBean.getContentType())){
				throw new BaseException("struts.messages.error.content.type.not.allowed");
			}
			String fileName = CommonUtils.getUploadFileName(communityBean.getStreetId(), Constant.IMG_COMMUNITY_PHOTO);
			communityBean.setCommunityPhoto(communityBean.getPhotoDir() + fileName);
			
			File target = new File(communityBean.getPhotoPath() + fileName);
			WriteFile wf = new WriteFile(fileBean.getFile(), target, fileSize);
			try {
				wf.write();
			} catch (BaseException e) {
				throw new BaseException("custom.messages.error.leader.upload.error");
			}
		}
		return communityBean;
	}
}
